package com.j2bugzilla.base;

import java.util.Date;
import java.util.Map;

public class BugHistoryFactory {

	public BugHistoryDetail createBugHistory(Map<String, Object> bugMap) {
		Integer id = (Integer) bugMap.get("id");
		String alias = (String) bugMap.get("alias");
		BugHistoryDetail bugHistory = new BugHistoryDetail(id, alias);
		BugHistoryDetail.History history;
		BugHistoryDetail.Change change;
		Object[] historyEntries = (Object[]) bugMap.get("history");
		for (Object hashObj : historyEntries) {
			Map<String, Object> hash = (Map<String, Object>) hashObj;
			history = new BugHistoryDetail.History();
			history.setWhen((Date)hash.get("when"));
			history.setWho((String) hash.get("who"));
			Object[] changeEntries = (Object[]) hash.get("changes");
			for (Object changeEntryObj : changeEntries) {
				Map<String, Object> changeEntry = (Map<String, Object>) changeEntryObj;
				change = new BugHistoryDetail.Change();
				change.setFieldName((String) changeEntry.get("field_name"));
				change.setRemoved((String) changeEntry.get("removed"));
				change.setAdded((String) changeEntry.get("added"));
				change.setAttachmentId((Integer) changeEntry.get("attachment_id"));
				history.addChange(change);
			}
			bugHistory.addHistory(history);
		}

		return bugHistory;
	}

}
